package com.klm.easymq.controller;

import com.klm.easymq.config.WebConsoleConfig;
import com.klm.easymq.service.EasyMqService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Easy-MQ Web控制台控制器
 * 提供Web界面的API接口
 */
@Slf4j
@RestController
@RequestMapping("/easymq/api/console")
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "easy-mq.console", name = "enabled", havingValue = "true")
public class EasyMqWebConsoleController {
    
    private final WebConsoleConfig consoleConfig;
    private final EasyMqService easyMqService;
    
    /**
     * 获取控制台配置信息
     */
    @GetMapping("/config")
    public Map<String, Object> getConfig() {
        Map<String, Object> config = new HashMap<>();
        config.put("enabled", consoleConfig.isEnabled());
        config.put("port", consoleConfig.getPort());
        config.put("contextPath", consoleConfig.getContextPath());
        config.put("securityEnabled", consoleConfig.isSecurityEnabled());
        config.put("theme", consoleConfig.getTheme());
        config.put("realtimeEnabled", consoleConfig.isRealtimeEnabled());
        config.put("refreshInterval", consoleConfig.getRefreshInterval());
        return config;
    }
    
    /**
     * 获取系统概览信息
     */
    @GetMapping("/overview")
    public Map<String, Object> getOverview() {
        Map<String, Object> overview = new HashMap<>();
        overview.put("healthy", easyMqService.isHealthy());
        overview.put("timestamp", System.currentTimeMillis());
        overview.put("version", "1.0.0");
        return overview;
    }
    
    /**
     * 获取实时监控数据
     */
    @GetMapping("/monitor/realtime")
    public Map<String, Object> getRealtimeData() {
        Map<String, Object> data = new HashMap<>();
        // 这里可以添加实时监控数据的获取逻辑
        data.put("timestamp", System.currentTimeMillis());
        data.put("activeConnections", 1);
        data.put("messageRate", 100);
        return data;
    }
    
    /**
     * 获取消息统计信息
     */
    @GetMapping("/messages/stats")
    public Map<String, Object> getMessageStats() {
        Map<String, Object> stats = new HashMap<>();
        // 这里可以添加消息统计信息的获取逻辑
        stats.put("totalSent", 1000);
        stats.put("totalConsumed", 950);
        stats.put("errorCount", 5);
        stats.put("successRate", 95.0);
        return stats;
    }
    
    /**
     * 获取主题列表
     */
    @GetMapping("/topics")
    public Map<String, Object> getTopics() {
        Map<String, Object> result = new HashMap<>();
        // 这里可以添加主题列表的获取逻辑
        result.put("topics", new String[]{"order.created", "order.updated", "inventory.changed"});
        return result;
    }
    
    /**
     * 发送测试消息
     */
    @PostMapping("/messages/test")
    public Map<String, Object> sendTestMessage(@RequestBody Map<String, Object> request) {
        Map<String, Object> result = new HashMap<>();
        try {
            String topic = (String) request.get("topic");
            String message = (String) request.get("message");
            
            // 发送测试消息
            boolean success = easyMqService.sendSync(topic, message);
            
            result.put("success", success);
            result.put("message", success ? "消息发送成功" : "消息发送失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "发送失败: " + e.getMessage());
        }
        return result;
    }
    
    /**
     * 获取系统日志
     */
    @GetMapping("/logs")
    public Map<String, Object> getLogs(@RequestParam(defaultValue = "100") int limit) {
        Map<String, Object> result = new HashMap<>();
        // 这里可以添加日志获取逻辑
        result.put("logs", new String[]{"[INFO] 系统启动成功", "[INFO] 连接RocketMQ成功"});
        result.put("total", 2);
        return result;
    }
} 